package com.iceGame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.MassData;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.Shape;
import com.badlogic.gdx.physics.box2d.World;




public class IceCube extends DynamicGameObject{
	Body body;
	public static final int NORMAL = 0;
	public static final int JUMPING = 0;
	public static final int JUMPVELOCITY = 6;
	public float time ;
	public float _width;
	public float _height;
	public float _fireBallReduction = 0.5f;
	public int state;
	public PhysicsWorld world;
	
	public IceCube(PhysicsWorld world,float x, float y, float width, float height) {
		super(x, y, width, height);
		setBody(world.world, x, y, width, height);
		time=0;
		state = NORMAL;
		this.world = world;
	}

	public void update(float deltaTime) {
		bounds.x = body.getPosition().x;
		bounds.y =body.getPosition().y;
		time+= deltaTime;
        if (time >= world.currentLevel.meltDownRate){
                time -= world.currentLevel.meltDownRate;
                if (this.bounds.width>0.40)
                	CubeReduction(false);
                else
                	world.state=world.WORLD_STATE_GAME_OVER;
        }	
        if (body.getLinearVelocity().y >= 0){
        	state = NORMAL;
        }
        
	}

	public void CubeReduction(boolean fireball) {
		Fixture oldFixture = body.getFixtureList().get(0);
		Shape shape = oldFixture.getShape();
		PolygonShape oldShape = (PolygonShape)shape;
		int VertexNum = oldShape.getVertexCount();
		Vector2 vertex0 = new Vector2();
		Vector2 vertex1 = new Vector2();
		Vector2 vertex2 = new Vector2();
		oldShape.getVertex(0, vertex0);
		oldShape.getVertex(1, vertex1);
		oldShape.getVertex(2, vertex2);
		float NewWidth;
		float NewHeight;
		if(fireball){
			NewWidth = ((vertex1.x-vertex0.x)/2*_fireBallReduction);
			NewHeight = ((vertex2.y-vertex0.y)/2*_fireBallReduction);
		}
		else{
			NewWidth = ((vertex1.x-vertex0.x)/2*world.currentLevel.meltAmount);
			NewHeight = ((vertex2.y-vertex0.y)/2*world.currentLevel.meltAmount);
		}
		//in order to render the cube
		this.bounds.width = NewWidth * 2;
		this.bounds.height = NewHeight*2;
		body.destroyFixture(oldFixture);

		PolygonShape newShape = new PolygonShape();
		newShape.setAsBox(NewWidth, NewHeight);
		body.createFixture(newShape, 1);	
	}
	
	public void CubeEnlarge() {
		Fixture oldFixture = body.getFixtureList().get(0);
		Shape shape = oldFixture.getShape();
		PolygonShape oldShape = (PolygonShape)shape;
		int VertexNum = oldShape.getVertexCount();
		Vector2 vertex0 = new Vector2();
		Vector2 vertex1 = new Vector2();
		Vector2 vertex2 = new Vector2();
		oldShape.getVertex(0, vertex0);
		oldShape.getVertex(1, vertex1);
		oldShape.getVertex(2, vertex2);
		float NewWidth = ((vertex1.x-vertex0.x)*(world.currentLevel.meltAmount*0.6f));
		float NewHeight = ((vertex2.y-vertex0.y)*(world.currentLevel.meltAmount*0.6f));
		//in order to render the cube
		this.bounds.width = NewWidth * 2;
		this.bounds.height = NewHeight*2;
		body.destroyFixture(oldFixture);

		PolygonShape newShape = new PolygonShape();
		newShape.setAsBox(NewWidth, NewHeight);
		body.createFixture(newShape, 1);

	
	}
	
	private void setBody(World world,float x,float y,float width,float height){
		//create the shape	
		PolygonShape shape = new PolygonShape();
		shape.setAsBox(width/2, height/2);		
		//create  FixtureDef	
		FixtureDef ShapeDef = new FixtureDef();
		ShapeDef.shape = shape;
		ShapeDef.filter.categoryBits=PhysicsWorld.CATEGORY_ICE;
		ShapeDef.filter.maskBits=PhysicsWorld.MASK_ICE;	
		//create  BodyDef	
		BodyDef bodydef = new BodyDef();
		bodydef.position.set(x,y);
		bodydef.type = BodyType.DynamicBody;
		// create Body
		body = world.createBody(bodydef);		
		body.createFixture(ShapeDef);
		
		MassData data = new MassData();
		data.mass = 5;
		data.I = 0.1f;
		body.setMassData(data);
	}

	public void jump() {
		state = JUMPING;
		body.setLinearVelocity(new Vector2(0,JUMPVELOCITY));
	}
	
	public boolean canJump(World world){
		for (int i=0;i<world.getContactCount();i++){
			if (world.getContactList().get(i).isTouching() && world.getContactList().get(i).getWorldManifold().getNormal().y<0 && world.getContactList().get(i).getWorldManifold().getNormal().y>=-1 && Math.abs(world.getContactList().get(i).getWorldManifold().getNormal().x)>=0 && Math.abs(world.getContactList().get(i).getWorldManifold().getNormal().x)<1)
			{
				return true;
			}
		}
		return false;	
		
	}

}
